# -*- coding: utf-8 -*-
#
# Copyright (C) 2009, Aşkın Yollu <askin@askin.ws>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#

import Image

class ConvertImage:
    def __init__(self, fileName):
        self.fileName = fileName
        self.img = Image.open(fileName)
        self.append = '.resized'
        self.saveOption = Image.ANTIALIAS
        self.width = 30
        self.height = 30

    def setOption(self, opt = "antialias"):
        ''' Method used when change saving option
        Options: "antialias", "cubic", "nearest", "linear"
        Default: "antialias" '''

        if saveOpt == "antialias":
            self.saveOption = Image.ANTIALIAS
        elif saveOpt == "cubic":
            self.saveOption = Image.BICUBIC
        elif saveOpt == "nearest":
            self.saveOption == Image.NEAREST
        elif saveOpt == "linear":
            self.saveOption = Image.BILINEAR
        else:
            self.saveOption = Image.ANTIALIAS

    def setSize(self, width, height):
        ''' Method used when change size '''

        if width <= 0 or height <= 0:
            print "Error, Wrong size"
        else:
            self.width  = width
            self.height = height

    def setSizePercent(self, percent):
        ''' Set size with given percent value '''
        self.width, self.height = self.img.size
        self.width  = self.width  * percent / 100
        self.height = self.height * percent / 100

    def resizeImage(self):
        img2 = self.img.resize((self.width, self.height), self.saveOption)
        newImageFile = self.fileName.rpartition('.')
        img2.save(newImageFile[0] + self.append + newImageFile[1] + newImageFile[2])

    def setAppend(self, append):
        self.append = append
